<?php
namespace App\Http\Controllers\api;
use App\Http\Controllers\Controller;
use App\Models\Member;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Models\Camel_user;
use App\Models\Wallet;
use Illuminate\Support\Facades\Input;
class UserLoginController extends Controller
{
    //用户登录
    public function userLogin(){
        $model = new Member;
        if (false !== $userInfo = $model->getUserInfo()) {
            if (($user = $model->isLogin($userInfo['unionId'])) !== null) {
                $info = $model::where('unionid', $userInfo['unionId'])->select('username','head_ico','open_id','api_token')->first();
                return $info;
            }
            return $model->createUser($userInfo);
        }
        return [];
    }

    //失败返回
    public function loginFailure(){
      return  response()->json(['status'=>0,'msg'=>'数据不存在！','data'=>'']);
    }

    //骆驼商城获取用户信息接口
    public function camelUserLogin(Request $request){

        $member = Member::where('open_id',$request->openid)->first();
        $user_wallet = Wallet::where('openid',$request->openid)->first();
       if($member){
           if(!$user_wallet){
               return response()->json(['status'=>0,'msg'=>'数据异常,该用户钱包不存在!','data'=>'']);
           }
           return ['money'=>number_format($user_wallet->money ,2, '.', '')];
       }else{
            $model = new Member;
            $userInfo =   Camel_user::where('open_id',$request->openid)->first();
            return $model->createUser($userInfo);
       }

    }

    //显示余额接口
    public function yue(Request $request)
    {
        if($request->openid){
            $res = Wallet::where('openid', $request->openid)->select('money')->first();
            if($res){
                return ['money'=>number_format($res->money ,2, '.', '')];
            }else{
                return "钱包不存在！";
            }
        }else{
            return "缺少openid!";
        }
    }

}